package com.hb.trade.api.routine.controller.flow;

import cn.hutool.core.util.StrUtil;
import com.hb.core.pojo.flow.dto.FlowApproverDTO;
import com.hb.core.pojo.flow.dto.FlowTempMainDTO;
import com.hb.core.pojo.flow.entity.FlowApprover;
import com.hb.trade.client.flow.FlowClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Slf4j
@Version(1.0)
@Api(tags = {"工作流 - 启动流程"})
@RequestMapping("/{version}")
public class ApiFlowController {

    protected final FlowClient flowClient;

    public ApiFlowController(FlowClient flowClient) {
        this.flowClient = flowClient;
    }

    @ApiOperation(value = "发起流程", notes = "发起流程", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "流程模板ID", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "formFields", value = "字段信息", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/flow/launch")
    public ResponseDto launch(@ApiIgnore @RequestBody FlowTempMainDTO flowTempMainDto) {
        Long currentUserId = ShiroUtil.getUserId();
        flowTempMainDto.setUserId(currentUserId);
        Message message = flowClient.launch(flowTempMainDto);
        return ResultUtil.msg(message);
    }

    @ApiOperation(value = "获取指定用户的待办列表", notes = "获取指定用户的待办列表", httpMethod = "POST")
    @PostMapping("/flow/todo")
    public ResponseDto todo(@ApiIgnore @RequestBody FlowApprover approver) {
        Long currentUserId = ShiroUtil.getUserId();
        approver.setApproverId(currentUserId);
        Message message = flowClient.todo(approver);
        return ResultUtil.msg(message);
    }

    @ApiOperation(value = "审批指定流程", notes = "审批指定流程", httpMethod = "POST")
    @PostMapping("/flow/audit")
    public ResponseDto audit(@ApiIgnore @RequestBody FlowApproverDTO approverDto) {
        String valid = approverDto.paramsValid();
        if (StrUtil.isNotEmpty(valid)) {
            return ResultUtil.fail(valid);
        }
        Long currentUserId = ShiroUtil.getUserId();
        approverDto.setApproverId(currentUserId);

        Message message = flowClient.todo(approverDto);
        return ResultUtil.msg(message);
    }
}
